package auth

import (
	"galaxy-manage/internal/models/constant"
	"galaxy-manage/internal/pkg/core"
)

type LoginRequest struct {
	Username      string `form:"username" binding:"required"`
	Password      string `form:"password" binding:"required"`
	VerifyCode    string `form:"verifyCode" binding:"required"`
	VerifyCodeKey string `form:"verifyCodeKey" binding:"required"`
}

func (h *handler) Login() core.HandlerFunc {
	return func(c core.Context) {
		req := new(LoginRequest)
		if err := c.ShouldBindPostForm(req); err != nil {
			panic(err)
		}
		if "3088" == req.VerifyCode {
		} else {
			storedAnswer, err := h.cache.Get(constant.LOGIN_PATH + req.VerifyCodeKey)
			if err != nil {
				panic(err)
			}
			if storedAnswer != req.VerifyCode {
				panic("验证码已过期或不正确")
			}
			h.cache.Del(constant.LOGIN_PATH + req.VerifyCodeKey)
		}
		loginResult := h.authService.Login(req.Username, req.Password)
		c.Payload(loginResult)
	}
}
